package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.R;
import com.example.practice.bean.ResultBean;
import com.example.practice.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class registerActivity_yl extends AppCompatActivity {
    private TextView tv_title_yl;
    private TextView tv_back_yl;
    private Button btn_register_yl;
    private EditText et_userName_yl, et_pwd_yl, et_pwd_again_yl;
    private String userName, pwd, pwdAgain;
    private RelativeLayout rl_title_bar_yl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_yl);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        initView();
    }

    private void initView() {
        tv_title_yl=findViewById(R.id.tv_title);
        tv_title_yl.setText("注册");
        tv_back_yl=findViewById(R.id.tv_back);
        rl_title_bar_yl=findViewById(R.id.rl_bar);
        rl_title_bar_yl.setBackgroundColor(Color.TRANSPARENT);
        btn_register_yl=findViewById(R.id.btn_register_yl);
        et_userName_yl=findViewById(R.id.et_userName_yl);
        et_pwd_yl=findViewById(R.id.et_password_yl);
        et_pwd_again_yl=findViewById(R.id.et_password_again_yl);

//        设置返回点击事件
        tv_back_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerActivity_yl.this.finish();
            }
        });

        // 注册按钮点击事件
        btn_register_yl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnClick();
            }
        });
    }

    /**
     * 获取控件的字符串
     */
    private void getEditString(){
        userName=et_userName_yl.getText().toString().trim();
        pwd=et_pwd_yl.getText().toString().trim();
        pwdAgain=et_pwd_again_yl.getText().toString().trim();
    }

    /**
     * 从Sp 中读取登录信息中的用户名,判断是否存在用户
     * @param userName
     * @return
     */
    private boolean isExistUserName(String userName){
        boolean has_userName=false;
        SharedPreferences sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spUser = sp.getString(userName,"");
        if(!TextUtils.isEmpty(spUser)){
            has_userName=true;
        }
        return has_userName;
    }

    /**
     * 保存账号到SP
     * @param userName
     * @param pwd
     */
    private void saveRegisterInfo(String userName, String pwd) {
        SharedPreferences sp =getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userName, pwd);
        editor.commit();
    }

    /**
     * 注册按钮功能实现
     */
    private void btnClick(){
        getEditString();
        if(TextUtils.isEmpty(userName)){
            Toast.makeText(registerActivity_yl.this,"请输入用户名",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(TextUtils.isEmpty(pwd)){
            Toast.makeText(registerActivity_yl.this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(TextUtils.isEmpty(pwdAgain)){
            Toast.makeText(registerActivity_yl.this,"请再次输入密码",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(!pwdAgain.equals(pwd)){
            Toast.makeText(registerActivity_yl.this,"输入两次的密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }
        else if(isExistUserName(userName)){
            Toast.makeText(registerActivity_yl.this,"用户名已存在",Toast.LENGTH_SHORT).show();
            return;
        }
        else {
//            Toast.makeText(registerActivity_yl.this,"注册成功",Toast.LENGTH_SHORT).show();
//            saveRegisterInfo(userName,pwd);

            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("userName",userName);
            jsonObject.addProperty("pwd", pwd);
            jsonObject.addProperty("kfz", "6");

            OkHttpUtils.sendOkHttp(false, true, "AndroidUser/reg", jsonObject, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String res = response.body().string();
                    Log.i("res", res);
                    Gson gson = new Gson();
                    final ResultBean<String> result =  gson.fromJson(res, ResultBean.class);

                    registerActivity_yl.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (result.code==200) {
                                Toast.makeText(registerActivity_yl.this,"注册成功",Toast.LENGTH_SHORT).show();
                                //注册成功后把账户名传递到loginActivity
                                Intent data = new Intent();
                                data.putExtra("userName",userName);
                                setResult(RESULT_OK,data);
                                registerActivity_yl.this.finish();
                            }
                        }
                    });
                }
            });
        }
    }
}